package com.srwl.mytx.utils;

import com.alibaba.fastjson.JSONObject;

import java.security.PrivateKey;
import java.security.PublicKey;

import javax.crypto.SecretKey;

public class HttpEncryptUtil {



    //APP加密请求内容
    public static String appEncrypt(String content, String servicePublicKey,String aesKeyStr) throws Exception {

        //将Base64编码后的Server公钥转换成PublicKey对象
        PublicKey serverPublicKey = RSAUtil.string2PublicKey(servicePublicKey);

       SecretKey aesKey = AESUtil.loadKeyAES(aesKeyStr);
        //用Server公钥加密AES秘钥
        byte[] encryptAesKey = RSAUtil.publicEncrypt(aesKeyStr.getBytes(), serverPublicKey);

        //用AES秘钥加密请求内容
        byte[] encryptRequest = AESUtil.encryptAES(content.getBytes(), aesKey);

        JSONObject result = new JSONObject();
        result.put("ak", RSAUtil.byte2Base64(encryptAesKey));
        result.put("ct", RSAUtil.byte2Base64(encryptRequest));
        return result.toJSONString();
    }

    //APP解密服务器的响应内容 ,aeskeyStr是客户端请求服务器时候生成的，当客户端响应数据后，用该aesKey解密即可，获得
    public static String appDecrypt(String content, String aesKeyStr) throws Exception {

        // String encryptAesKeyStr = (String) result.get("ak");
        // String encryptContent = content.getString("ct");

        //将Base64编码后的APP私钥转换成PrivateKey对象
        // PrivateKey appPrivateKey = RSAUtil.string2PrivateKey(appPrivateKeyStr);
        //用APP私钥解密AES秘钥
        // byte[] aesKeyBytes = RSAUtil.privateDecrypt(RSAUtil.base642Byte(encryptAesKeyStr), appPrivateKey);
        //用AES秘钥解密请求内容
        SecretKey aesKey = AESUtil.loadKeyAES(aesKeyStr);
        byte[] response = AESUtil.decryptAES(RSAUtil.base642Byte(content), aesKey);
        return new String(response);

    }

    //服务器加密响应给APP的内容
    public static String serverEncrypt(String appPublicKeyStr, String aesKeyStr, String content) throws Exception {
        //将Base64编码后的APP公钥转换成PublicKey对象
        PublicKey appPublicKey = RSAUtil.string2PublicKey(appPublicKeyStr);
        //将Base64编码后的AES秘钥转换成SecretKey对象
        SecretKey aesKey = AESUtil.loadKeyAES(aesKeyStr);
        //用APP公钥加密AES秘钥
        byte[] encryptAesKey = RSAUtil.publicEncrypt(aesKeyStr.getBytes(), appPublicKey);
        //用AES秘钥加密响应内容
        byte[] encryptContent = AESUtil.encryptAES(content.getBytes(), aesKey);

        JSONObject result = new JSONObject();
        result.put("ak", RSAUtil.byte2Base64(encryptAesKey).replaceAll("\r\n", ""));
        result.put("ct", RSAUtil.byte2Base64(encryptContent).replaceAll("\r\n", ""));
        return result.toString();
    }

    //服务器解密APP的请求内容
    public static String serverDecrypt(String content, String servicePricateKey) throws Exception {
        JSONObject result = JSONObject.parseObject(content);
        String encryptAesKeyStr = (String) result.get("ak");
        String encryptContent = (String) result.get("ct");

        //将Base64编码后的Server私钥转换成PrivateKey对象
        PrivateKey serverPrivateKey = RSAUtil.string2PrivateKey(servicePricateKey);
        //用Server私钥解密AES秘钥
        byte[] aesKeyBytes = RSAUtil.privateDecrypt(RSAUtil.base642Byte(encryptAesKeyStr), serverPrivateKey);
        //生成AES秘钥
        SecretKey aesKey = AESUtil.loadKeyAES(new String(aesKeyBytes));

        //用AES秘钥解密请求内容
        byte[] request = AESUtil.decryptAES(RSAUtil.base642Byte(encryptContent), aesKey);
        String s = new String(request);
        JSONObject result2 = new JSONObject();
        result2.put("ak", new String(aesKeyBytes));
        result2.put("ct", JSONObject.parseObject(s));
        return result2.toString();
    }

}
